package com.ericsson.entironment.bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ericsson.common.db.DatabaseMng;
import com.ericsson.common.exception.SmafException;
import com.ericsson.entironment.manager.FrameManager;
/**
 * 
 * <p>Title: EntironmentMonitor</p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: GentleSoft</p>
 * 
 * <p>Description: 终端数据对象</p> 
 * @author Oarsman
 * @version 1.0 Sep 19, 2009
 */
public class TerminalBean
{
	protected String trmSequence;
	protected String trmname;
	protected String trmtypeid;
	protected String trmcomid;
	protected String trmcomname;
	protected String temimsi;
	protected String trmtel;
	protected String msgdateid;
	protected Map<String,String> frames=null;
	private static Log log=LogFactory.getLog(TerminalBean.class);
	/**
	 * 根据序列号载入终端的基本信息
	 * @param sequence
	 */
	public void load(String sequence)
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
			sql="select * from EMA_Terminal where trmSequence='"+sequence+"'";
			rst=stmt.executeQuery(sql);
			
			if (rst.next())
			{//有数据
				this.trmSequence=rst.getString("trmsequence");
				this.trmname=rst.getString("trmname");
				this.trmtypeid=rst.getString("trmtypeid");
				this.trmcomname=rst.getString("trmcomname");
				this.trmcomid=rst.getString("trmcomid");
				this.temimsi=rst.getString("temimsi");
				this.trmtel=rst.getString("trmtel");
				this.msgdateid=rst.getString("msgdateid");
			}
			else
			{//无匹配记录
				this.trmSequence=null;
			}
			

		}
		catch (Exception e)
		{
			log.error("execute sql error ["+sql+"]",e);
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
	}

	public void save(String hsid) throws SmafException
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			conn.setAutoCommit(false);
			stmt = conn.createStatement();
			sql="insert into EMA_Terminal(trmSequence,trmname,trmtypeid,trmcomid,trmcomname,temimsi,trmtel,msgdateid)"
				+"values ('"+this.trmSequence+"','"+this.trmname+"','"+this.trmtypeid+"','"+this.trmcomid+"'"
				+",'"+this.trmcomname+"','"+this.temimsi+"','"+this.trmtel+"','"+this.msgdateid+"')";
			stmt.executeUpdate(sql);
			sql="insert into ema_terminalinstall(trmSequence,hsid) values ('"+this.trmSequence+"','"+hsid+"')";
			stmt.executeUpdate(sql);
			//初始化终端所有的参数
			sql="insert into ema_valuelimit(trmSequence,tag,framename) values ('"+this.trmSequence+"','100','应用服务器地址')";
			stmt.executeUpdate(sql);
			sql="insert into ema_valuelimit(trmSequence,tag,framename) values ('"+this.trmSequence+"','95','网络接入APN')";
			stmt.executeUpdate(sql);
			sql="insert into ema_valuelimit(trmSequence,tag,framename) values ('"+this.trmSequence+"','90','网络拨号号码')";
			stmt.executeUpdate(sql);
			sql="insert into ema_valuelimit(trmSequence,tag,framename) values ('"+this.trmSequence+"','85','服务器通信端口')";
			stmt.executeUpdate(sql);
			sql="insert into ema_valuelimit(trmSequence,tag,framename) values ('"+this.trmSequence+"','83','短信接收号码')";
			stmt.executeUpdate(sql);
			sql="insert into ema_valuelimit(trmSequence,tag,framename) values ('"+this.trmSequence+"','80','串口数据波特率')";
			stmt.executeUpdate(sql);
			sql="insert into ema_valuelimit(trmSequence,tag,framename) values ('"+this.trmSequence+"','75','最大错误重发次数')";
			stmt.executeUpdate(sql);
			sql="insert into ema_valuelimit(trmSequence,tag,framename) values ('"+this.trmSequence+"','70','消息重发间隔')";
			stmt.executeUpdate(sql);
			sql="insert into ema_valuelimit(trmSequence,tag,framename) values ('"+this.trmSequence+"','65','心跳消息间隔')";
			stmt.executeUpdate(sql);
			//门限内容
			loadFrames();
			Iterator key=this.frames.keySet().iterator();
			String id;
			while(key.hasNext())
			{
				id=(String) key.next();
				sql="insert into ema_valuelimit(trmSequence,tag,frameid,framename,framevalue) values ('"+this.trmSequence+"','0"+id+"','"+id+"','"+frames.get(id)+"值上限','60')";
				stmt.executeUpdate(sql);
				sql="insert into ema_valuelimit(trmSequence,tag,frameid,framename,framevalue) values ('"+this.trmSequence+"','00"+id+"','"+id+"','"+frames.get(id)+"值下限','20')";
				stmt.executeUpdate(sql);
			}
			
			conn.commit();
			conn.setAutoCommit(true);
		}
		catch (Exception e)
		{
			try
			{
				conn.rollback();
			}
			catch (SQLException e1)
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			log.error("execute sql error ["+sql+"]",e);
			throw new SmafException(e);
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
	}
	

	/**
	 * 获取帧号对应的数据名称
	 * @param frameid
	 * @return
	 */
	public String getFrameName(String frameid)
	{
		if (frames==null)
		{//第一次使用，载入帧数据信息
			
			loadFrames();
		}
		
		return frames.get(frameid);
		
	}

	/**
	 * 删除当前终端
	 * @throws SmafException 
	 *
	 */
	public void delete() throws SmafException
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
			sql="delete from EMA_Terminal where trmSequence='"+this.trmSequence+"'";
			stmt.executeUpdate(sql);
		}
		catch (Exception e)
		{
			log.error("execute sql error ["+sql+"]",e);
			throw new SmafException("系统错误，无法完成终端的删除");
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
		
	}
	
	/**
	 *  载入终端对应的帧数据信息
	 *
	 */
	private void loadFrames()
	{
		frames=FrameManager.getFrameInfo(this.msgdateid);
	}

	public static Log getLog()
	{
		return log;
	}

	public static void setLog(Log log)
	{
		TerminalBean.log = log;
	}

	public Map<String, String> getFrames()
	{
		return frames;
	}

	public void setFrames(Map<String, String> frames)
	{
		this.frames = frames;
	}

	public String getMsgdateid()
	{
		return msgdateid;
	}

	public void setMsgdateid(String msgdateid)
	{
		this.msgdateid = msgdateid;
	}

	public String getTemimsi()
	{
		return temimsi;
	}

	public void setTemimsi(String temimsi)
	{
		this.temimsi = temimsi;
	}

	public String getTrmcomid()
	{
		return trmcomid;
	}

	public void setTrmcomid(String trmcomid)
	{
		this.trmcomid = trmcomid;
	}

	public String getTrmcomname()
	{
		return trmcomname;
	}

	public void setTrmcomname(String trmcomname)
	{
		this.trmcomname = trmcomname;
	}

	public String getTrmname()
	{
		return trmname;
	}

	public void setTrmname(String trmname)
	{
		this.trmname = trmname;
	}

	public String getTrmSequence()
	{
		return trmSequence;
	}

	public void setTrmSequence(String trmSequence)
	{
		this.trmSequence = trmSequence;
	}

	public String getTrmtel()
	{
		return trmtel;
	}

	public void setTrmtel(String trmtel)
	{
		this.trmtel = trmtel;
	}

	public String getTrmtypeid()
	{
		return trmtypeid;
	}

	public void setTrmtypeid(String trmtypeid)
	{
		this.trmtypeid = trmtypeid;
	}

	
	
}
